import 'package:flutter/material.dart';
import 'package:sow_app_core/sow_app_core.dart';
import 'package:sow_app_i18n/sow_app_i18n.dart';

import '../../../../../presenter/state_register_presenter.dart';
import '../login_page.dart';
import 'term_of_service.dart';

class RegisterPage extends StatefulWidget {
  static const routeName = '${LoginPage.routeName}/login';
  const RegisterPage(
      {super.key, required this.presenter, required this.controller});

  final StateRegisterPresenter presenter;
  final RegisterController controller;
  @override
  RegisterPageState createState() {
    return RegisterPageState();
  }
}

class RegisterPageState extends State<RegisterPage> {
  final _formKey = GlobalKey<FormState>();

  final _termOfServiceKey = GlobalKey<TermOfServiceState>();

  @override
  void initState() {
    widget.presenter.presenterContext = context;
    widget.presenter.presenterSetState = setState;
    widget.controller.getCaptcha();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(I18n.register),
      ),
      body: Stack(
        children: [
          Center(
              child: Column(
            children: [
              const Spacer(
                flex: 3,
              ),
              Icon(
                Icons.signpost_outlined,
                color: Colors.grey.shade300,
                size: 240,
              ),
              const Spacer(
                flex: 1,
              ),
            ],
          )),
          Form(
            key: _formKey,
            child: Column(
              children: [
                TextFormField(
                  keyboardType: TextInputType.phone,
                  decoration: const InputDecoration(
                    border: UnderlineInputBorder(),
                    labelText: I18n.phone_number,
                  ),
                  validator: (value) {
                    if (value == null ||
                        value.isEmpty ||
                        !RegExp(I18n.phone_regex).hasMatch(value)) {
                      return I18n.invalid_username;
                    }
                    widget.presenter.username = value;
                    return null;
                  },
                ),
                TextFormField(
                  keyboardType: TextInputType.visiblePassword,
                  obscureText: true,
                  decoration: const InputDecoration(
                    border: UnderlineInputBorder(),
                    labelText: I18n.password,
                  ),
                  validator: (value) {
                    if (value == null ||
                        value.isEmpty ||
                        !RegExp(I18n.password_regex).hasMatch(value)) {
                      return I18n.invalid_password;
                    }
                    widget.presenter.password = value;
                    return null;
                  },
                ),
                TextFormField(
                  keyboardType: TextInputType.visiblePassword,
                  obscureText: true,
                  decoration: const InputDecoration(
                    border: UnderlineInputBorder(),
                    labelText: I18n.confirm_password,
                  ),
                  validator: (value) {
                    if (value == null ||
                        value.isEmpty ||
                        !RegExp(I18n.password_regex).hasMatch(value) ||
                        value != widget.presenter.password) {
                      return I18n.invalid_confirm_password;
                    }
                    return null;
                  },
                ),
                TextFormField(
                  keyboardType: TextInputType.number,
                  decoration: const InputDecoration(
                    border: UnderlineInputBorder(),
                    labelText: I18n.captcha,
                  ),
                  validator: (value) {
                    if (value == null ||
                        value.isEmpty ||
                        !RegExp(I18n.captcha_regex).hasMatch(value)) {
                      return I18n.invalid_captcha;
                    }
                    widget.presenter.captchaCode = value;
                    return null;
                  },
                ),
                TextButton(
                    onPressed: () {
                      widget.controller.getCaptcha();
                    },
                    child: Image.memory(
                      widget.presenter.captcha,
                    )),
                TermOfService(
                  key: _termOfServiceKey,
                ),
                ElevatedButton(
                  onPressed: () {
                    if (_formKey.currentState!.validate()) {
                      if (_termOfServiceKey.currentState?.accepted ?? false) {
                        widget.controller.register();
                      } else {
                        _termOfServiceKey.currentState?.controller
                            .forward(from: 0);
                      }
                    }
                  },
                  child: const Text(I18n.register),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
